using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FourthWallProject.GameObjects.Screens;
using FourthWallProject.GameObjects;

namespace FourthWallProject
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class ScreenManager : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager Graphics;
        public static SpriteBatch SpriteBatch;

        public ScreenManager()
        {
            Graphics = new GraphicsDeviceManager(this);
            Graphics.PreferredBackBufferHeight = 480;
            Graphics.PreferredBackBufferWidth = 800;
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.IsMouseVisible = true;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            GameState.MenuFont = Content.Load<SpriteFont>("Fonts/MainMenuFont");
            SpriteBatch = new SpriteBatch(GraphicsDevice);
            var screen = new MenuScreen(this);
            AddScreen(screen);
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        public static void AddScreen(GameScreen screen, bool isActive = true)
        {
            screen.IsActive = isActive;
            GameState.GameScreens.Add(screen);
        }

        public static void PauseScreen(GameScreen screen)
        {
            var sceen = GameState.GameScreens.Where(x => x == screen).SingleOrDefault();
            if (screen != null)
                screen.IsActive = false;
        }

        public static void UnpauseScreen(GameScreen screen)
        {
            var sceen = GameState.GameScreens.Where(x => x == screen).SingleOrDefault();
            if (screen != null)
                screen.IsActive = true;
        }

        public static void RemoveScreen(GameScreen screen)
        {
            GameState.GameScreens.Remove(screen);
        }

        protected override void Update(GameTime gameTime)
        {
            for (int screen = 0; screen < GameState.GameScreens.Count(); screen++)
            {
                var gameScreen = GameState.GameScreens[screen];
                if (gameScreen.IsActive)
                    gameScreen.Update(gameTime);
            }
            
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            if (GameState.GameScreens.Where(x => x.IsActive).Count() == 0)
            {
            }
            foreach (GameScreen gameScreen in GameState.GameScreens)
            {
                if(gameScreen.IsActive)
                    gameScreen.Draw(gameTime,SpriteBatch);
            }

            base.Draw(gameTime);
        }
    }
}
